package com.ksd.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ksd.common.annotation.Log;
import com.ksd.common.core.controller.BaseController;
import com.ksd.common.core.domain.AjaxResult;
import com.ksd.common.core.page.TableDataInfo;
import com.ksd.common.enums.BusinessType;
import com.ksd.common.enums.UserStatus;
import com.ksd.common.utils.poi.ExcelUtil;
import com.ksd.system.domain.SysTvData;
import com.ksd.system.service.ISysOperLogService;
import com.ksd.system.service.ISysTvDataService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ksd.system.domain.SysTvType;
import com.ksd.system.service.ISysTvTypeService;

/**
 * 内容分类配置Controller
 * 
 * @author y.wu
 * @date 2025-02-26
 */
@RestController
@RequestMapping("/system/tvType")
public class SysTvTypeController extends BaseController
{

    @Autowired
    private ISysTvTypeService sysTvTypeService;
    @Autowired
    private ISysTvDataService sysTvDataService;
    @Autowired
    private ISysOperLogService sysOperLogService;
    /**
     * 查询内容分类配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:tvType:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysTvType sysTvType)
    {
        startPage();
        List<SysTvType> list = sysTvTypeService.selectSysTvTypeList(sysTvType);
        return getDataTable(list);
    }

    /**
     * 导出内容分类配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:tvType:export')")
    @Log(title = "内容分类配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysTvType sysTvType)
    {
        List<SysTvType> list = sysTvTypeService.selectSysTvTypeList(sysTvType);
        ExcelUtil<SysTvType> util = new ExcelUtil<SysTvType>(SysTvType.class);
        util.exportExcel(response, list, "内容分类配置数据");
    }

    @PostMapping("/getContentTypeList")
    public AjaxResult getContentTypeList(){

        SysTvType sysTvType = new SysTvType();
        sysTvType.setStatus(UserStatus.OK.getCode());
        List<SysTvType> sysTvTypes = sysTvTypeService.selectSysTvTypeList(sysTvType);
        return success(sysTvTypes);
    }

    /**
     * 获取内容分类配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:tvType:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysTvTypeService.selectSysTvTypeById(id));
    }

    /**
     * 新增内容分类配置
     */
    @PreAuthorize("@ss.hasPermi('system:tvType:add')")
    @Log(title = "内容分类配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysTvType sysTvType)
    {
        if (!sysTvTypeService.checkNameUnique(sysTvType))
        {
            sysOperLogService.insertOperlogError("新增内容分类'" + sysTvType.getName() + "'失败，该名称已存在");
            return error("新增内容分类'" + sysTvType.getName() + "'失败，该名称已存在");
        }
        sysOperLogService.insertOperlogSuccess("新增内容分类'" + sysTvType.getName() + "'成功");
        return toAjax(sysTvTypeService.insertSysTvType(sysTvType));
    }

    /**
     * 修改内容分类配置
     */
    @PreAuthorize("@ss.hasPermi('system:tvType:edit')")
    @Log(title = "内容分类配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysTvType sysTvType)
    {
        if (!sysTvTypeService.checkNameUnique(sysTvType))
        {
            sysOperLogService.insertOperlogError("修改内容分类'" + sysTvType.getName() + "'失败，该名称已存在");
            return error("修改内容分类'" + sysTvType.getName() + "'失败，该名称已存在");
        }
        sysOperLogService.insertOperlogSuccess("修改内容分类'" + sysTvType.getName() + "'成功");
        return toAjax(sysTvTypeService.updateSysTvType(sysTvType));
    }

    /**
     * 删除内容分类配置
     */
    @PreAuthorize("@ss.hasPermi('system:tvType:remove')")
    @Log(title = "内容分类配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for (Long id : ids) {
            System.out.println("id:"+id+"============"+id);
            if (SysTvType.movieDefaultId.equals(id)){
                sysOperLogService.insertOperlogError("删除内容分类: 电影 失败,不允许该内容分类被删除");
                return error("删除内容分类: 电影 失败,不允许该内容分类被删除");
            }
            if (SysTvType.songDefaultId.equals(id)){
                sysOperLogService.insertOperlogError("删除内容分类: 歌曲 失败,不允许该内容分类被删除");
                return error("删除内容分类: 歌曲 失败,不允许该内容分类被删除");
            }
            SysTvType sysTvType = sysTvTypeService.selectSysTvTypeById(id);

            SysTvData sysTvData = new SysTvData();
            sysTvData.setTypeId(id);
            List<SysTvData> sysTvDataList = sysTvDataService.selectSysTvDataList(sysTvData);
            for (SysTvData tvData : sysTvDataList) {
                sysTvDataService.deleteSysTvDataById(tvData.getId());
            }
            sysTvTypeService.deleteSysTvTypeById(id);
            sysOperLogService.insertOperlogSuccess("删除内容分类: "+sysTvType.getName()+" 成功");

        }
        return success("删除成功");
    }
}
